package com.mangguo.tingjian.statistics.mapper;

import com.mangguo.tingjian.statistics.dto.RetentionRateDTO;
import com.mangguo.tingjian.statistics.dto.RetentionRateLineQuery;
import com.mangguo.tingjian.statistics.entity.DwOpenApp;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mangguo.tingjian.statistics.req.RetentionRateLineReq;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author auto
 * @since 2019-12-01
 */
@Mapper
public interface DwOpenAppMapper extends BaseMapper<DwOpenApp> {

    /**
     * 获得留存率数据
     * @return
     */
    List<RetentionRateDTO> getRetentionRateLines(@Param("params") RetentionRateLineReq query);

    @Select("<script>"
            + "SELECT DISTINCT imei "
            + "FROM  ${retentionRateLineReq.tableName} "
            + "<where> "
            + "  mDate = ${mDate} AND "

            + "<if test='retentionRateLineReq.getOsid() != null and retentionRateLineReq.getOsid().size()>0'> "
            + "osid IN "
            + "<foreach item='osid' collection='retentionRateLineReq.osid' open='(' close=')' separator=','> "
            + "#{osid}"
            + "</foreach> AND "
            + "</if>"

            + "<if test='retentionRateLineReq.getChannel() != null and retentionRateLineReq.getChannel().size()>0'> "
            + "(channelId IN "
            + "<foreach item='channel' collection='retentionRateLineReq.channel' open='(' close=')' separator=','> "
            + "#{channel}"
            + "</foreach> OR subChannelId IN"
            + "<foreach item='channel' collection='retentionRateLineReq.channel' open='(' close=')' separator=','> "
            + "#{channel}"
            +  "</foreach> )AND "
            + "</if>"


            + "<if test='(retentionRateLineReq.getCity() != null and retentionRateLineReq.getCity().size()>0) and (retentionRateLineReq.getProvince() != null and retentionRateLineReq.getProvince().size()>0) and (retentionRateLineReq.getCity().get(0) != -1 and retentionRateLineReq.getProvince().get(0) != -1) '> "
            + "(city IN "
            + "<foreach item='city' collection='retentionRateLineReq.city' open='(' close=')' separator=','> "
            + "#{city}"
            + "</foreach> OR province IN"
            + "<foreach item='province' collection='retentionRateLineReq.province' open='(' close=')' separator=','> "
            + "#{province}"
            +  "</foreach> )AND "
            + "</if>"
            + "1 = 1"
            + "</where>"
            + "</script>")

    List<String> getImeiBetweenDate(@Param("retentionRateLineReq")RetentionRateLineReq retentionRateLineReq, @Param("mDate")Integer mDate);


    @Select("<script>select count(1) as count" +
            " from dw_open where mdate = #{mDate} and " +
            "<if test='imeis != null and imeis.size()>0'> "+
            "imei IN " +
            "<foreach item='imei' collection='imeis' open='(' close=')' separator=','> "+
            "#{imei}"+
            "</foreach> AND "+
            "</if>"+
            "1 = 1 " +
            "</script>")
    Long countImeiByDateAndImei(@Param("mDate")Integer mDate, @Param("imeis") List<String> imeis);
}
